/**
 *  Copyright (c)  2011-2020 Panguso, Inc.
 *  All rights reserved.
 *
 *  This software is the confidential and proprietary information of Panguso, 
 *  Inc. ("Confidential Information"). You shall not
 *  disclose such Confidential Information and shall use it only in
 *  accordance with the terms of the license agreement you entered into with Panguso.
 */

package storage.dbhelper;

import cn.edu.thu.log.web.service.PropertiesReaderServices;
import cn.edu.thu.log.web.service.impl.PropertiesReaderServicesImpl;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.cassandra.thrift.ColumnOrSuperColumn;
import org.apache.cassandra.thrift.KeySlice;
import storage.dbobject.DbVersion;
import storage.util.StorageManager;
/**
 * 
 * @author fenyo rome
 *
 */
public class VersionHelper {

	/**
	 * 将version保存到LaUDMS数据库中
	 * 
	 * @param version
	 *            要存储的模型对象
	 */
	public void saveVersion(DbVersion version) {
		// StorageManager sm = new StorageManager(StorageParameter.db_keySpace,
		// StorageParameter.db_columnFamily_version);
		PropertiesReaderServices x1 = PropertiesReaderServicesImpl.instance();
		StorageManager sm = new StorageManager(x1.getProperties("db_keySpace"),
				x1.getProperties("db_columnFamily_version"));
		// sm.open();
		sm.setRowKey(version.getRowKey());
		sm.put(DbVersion.CL_VERSIONNUM, version.getVersionNum());
		sm.put(DbVersion.CL_UPDATETIME, version.getUpdateTime());
		sm.put(DbVersion.CL_VALIDSTARTTIME, version.getValidStartTime());
		sm.put(DbVersion.CL_VALIDENDTIME, version.getValidEndTime());
		ArrayList<String> logHeadList = version.getLogHeadList();
		for (int i = 0; i < logHeadList.size(); i++) {
			sm.put(DbVersion.CL_LOGHEAD + "_" + logHeadList.get(i),
					logHeadList.get(i));
		}
		// sm.close();

	}
	/**
	 * 
	 * @param version
	 * version
	 */
	public void updateVersion(DbVersion version) {
		PropertiesReaderServices x1 = PropertiesReaderServicesImpl.instance();
		StorageManager sm = new StorageManager(x1.getProperties("db_keySpace"),
				x1.getProperties("db_columnFamily_version"));
		// sm.open();
		sm.delete(version.getRowKey());
		sm.setRowKey(version.getRowKey());
		sm.put(DbVersion.CL_VERSIONNUM, version.getVersionNum());
		sm.put(DbVersion.CL_UPDATETIME, version.getUpdateTime());
		sm.put(DbVersion.CL_VALIDSTARTTIME, version.getValidStartTime());
		sm.put(DbVersion.CL_VALIDENDTIME, version.getValidEndTime());
		ArrayList<String> logHeadList = version.getLogHeadList();
		for (int i = 0; i < logHeadList.size(); i++) {
			sm.put(DbVersion.CL_LOGHEAD + "_" + logHeadList.get(i),
					logHeadList.get(i));
		}
		// sm.close();

	}

	/**
	 * 将一系列logHead保存到LaUDMS数据库中
	 * 
	 * @param versions
	 *            要存储的模型对象列表
	 */
	public void saveVersions(List<DbVersion> versions) {
		PropertiesReaderServices x1 = PropertiesReaderServicesImpl.instance();
		StorageManager sm = new StorageManager(x1.getProperties("db_keySpace"),
				x1.getProperties("db_columnFamily_version"));
		// sm.open();
		for (int i = 0; i < versions.size(); i++) {
			DbVersion version = versions.get(i);
			sm.setRowKey(version.getRowKey());
			sm.put(DbVersion.CL_VERSIONNUM, version.getVersionNum());
			sm.put(DbVersion.CL_UPDATETIME, version.getUpdateTime());
			sm.put(DbVersion.CL_VALIDSTARTTIME, version.getValidStartTime());
			sm.put(DbVersion.CL_VALIDENDTIME, version.getValidEndTime());
			ArrayList<String> logHeadList = version.getLogHeadList();
			for (int j = 0; j < logHeadList.size(); j++) {
				sm.put(DbVersion.CL_LOGHEAD + "_" + logHeadList.get(j),
						logHeadList.get(j));

			}
			// sm.close();
		}
	}
	/**
	 * 
	 * @param columnName
	 * columnName
	 * @param value
	 * value
	 * @return
	 */
	public List<DbVersion> getVersions(final String columnName,
			final String value) {
		List<DbVersion> result = new LinkedList<DbVersion>();
		PropertiesReaderServices x1 = PropertiesReaderServicesImpl.instance();
		StorageManager sm = new StorageManager(x1.getProperties("db_keySpace"),
				x1.getProperties("db_columnFamily_version"));
		// sm.open();
		List<KeySlice> crs = sm.get(columnName, value);

		if (crs == null || crs.size() == 0) {
			System.out.println("version value:" + value);
		}
		try {
			for (KeySlice ks : crs) {
				String rowKey = new String(ks.getKey());
				String versionNum = null, validStartTime = null, validEndTime = null, updateTime = null;
				ArrayList<String> logHeadList = new ArrayList();
				for (ColumnOrSuperColumn column : ks.getColumns()) {
					String columnsName = new String(column.getColumn()
							.getName());
					String columnsValue = new String(column.getColumn()
							.getValue());
					if (columnsName.equals(DbVersion.CL_VERSIONNUM)) {
						versionNum = columnsValue;
					} else if (columnsName.equals(DbVersion.CL_VALIDSTARTTIME)) {
						validStartTime = columnsValue;
					} else if (columnsName.equals(DbVersion.CL_VALIDENDTIME)) {
						validEndTime = columnsValue;
					} else if (columnsName.equals(DbVersion.CL_UPDATETIME)) {
						updateTime = columnsValue;
					} else if (columnsName.startsWith(DbVersion.CL_LOGHEAD)) {
						logHeadList.add(columnsValue);
					}
				}
				DbVersion pVersion = new DbVersion(rowKey, versionNum,
						validStartTime, validEndTime, updateTime, logHeadList);
				result.add(pVersion);
			}
		} catch (Exception ex) {
			Logger.getLogger(DbVersion.class.getName()).log(Level.SEVERE, null,
					ex);
		} 
		return result;
	}
	/**
	 * 
	 * @return ArrayList<String>
	 */
	public ArrayList<String> getAllVersionName() {
		PropertiesReaderServices x1 = PropertiesReaderServicesImpl.instance();
		StorageManager sm = new StorageManager(x1.getProperties("db_keySpace"),
				x1.getProperties("db_columnFamily_version"));
		// sm.open();
		List<KeySlice> crs = sm.getAll();
		try {
			Thread.sleep(10000);
		} catch (Exception e) {
			e.printStackTrace();
		}

		ArrayList<String> versionNumList = new ArrayList<String>();
		try {
			for (KeySlice ks : crs) {
				String versionNum = "";
				for (ColumnOrSuperColumn column : ks.getColumns()) {
					String columnsName = new String(column.getColumn()
							.getName());
					String columnsValue = new String(column.getColumn()
							.getValue());
					if (columnsName.equals(DbVersion.CL_VERSIONNUM)) {
						versionNum = columnsValue;
					}
				}
				if (!versionNumList.contains(versionNum)
						&& versionNum.length() > 0) {
					versionNumList.add(versionNum);
				}
			}
		} catch (Exception ex) {
			Logger.getLogger(DbVersion.class.getName()).log(Level.SEVERE, null,
					ex);
		} 
		return versionNumList;
	}

	/**
	 * 
	 * @param rowKey
	 * rowKey
	 */
	public void deleteVersion(final String rowKey) {
		PropertiesReaderServices x1 = PropertiesReaderServicesImpl.instance();
		StorageManager sm = new StorageManager(x1.getProperties("db_keySpace"),
				x1.getProperties("db_columnFamily_version"));
		// sm.open();
		sm.delete(rowKey);
		// sm.close();
	}

	/**
	 * 为产品添加日志体字段
	 * 
	 * @param version
	 *            产品
	 * @param logHead
	 *            日志体字段
	 */
	public void addLogHead(DbVersion version, String logHead) {
		PropertiesReaderServices x1 = PropertiesReaderServicesImpl.instance();
		StorageManager sm = new StorageManager(x1.getProperties("db_keySpace"),
				x1.getProperties("db_columnFamily_version"));
		// sm.open();
		sm.setRowKey(version.getRowKey());
		sm.put(DbVersion.CL_LOGHEAD + "_" + logHead, logHead);
		// sm.close();
	}

	
	/**
	 * 
	 * @param versionName
	 * versionName
	 * @param logBodyname
	 * logBodyname
	 */
	public void deleteLogBody(String versionName, String logBodyname) {
		throw new UnsupportedOperationException("Not yet implemented");
	}

	

}
